//
//  SetViewController.m
//  Nurse
//
//  Created by Sunny on 13-12-13.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "SetViewController.h"
#import "SettingCell.h"
#import "AddSignsViewController.h"
#import "NurseService.h"

@interface SetViewController ()
@property (nonatomic, strong) NSMutableArray *muarrSetting;

@end

@implementation SetViewController
@synthesize muarrSetting = _muarrSetting;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title  = @"设置";
    self.Buexit.backgroundColor=[UIColor colorWithRed:76/255.0 green:214/255.0 blue:193/255.0 alpha:1.0];
    
    // Do any additional setup after loading the view from its nib.
    [App showTabbar];
//    [self.tableVSetting setDelegate:(id<UITableViewDelegate>)self];
//    [self.tableVSetting setDataSource:(id<UITableViewDataSource>)self];
//    _muarrSetting = [NSMutableArray array];
//    NSMutableArray *muarr1 = [NSMutableArray array];
//    for (int i = 0; i < 4; i ++) {
//        [muarr1 addObject:[NSString stringWithFormat:@"设置选项%d",i]];
//    }
//    [_muarrSetting addObject:muarr1];
//    
//    NSMutableArray *muarr2 = [NSMutableArray array];
//    for (int j = 0; j < 2; j ++) {
//        [muarr2 addObject:[NSString stringWithFormat:@"更多选项%d",j]];
//    }
//    [_muarrSetting addObject:muarr2];
    [self setNav];
    
    [[NurseService sharedInstance]querySZ:@"体温单类型" withSuccessHandler:^(id result) {
        //[[DataEngine sharedInstance]showMessage:@"系统设置读取成功" withTitle:@"系统设置成功"];
        
        
        //[self.navigationController popViewControllerAnimated:YES];
        if ([result isKindOfClass:[NSArray class]]) {
            //[this.table reloadData];
            id record = [result objectAtIndex:0];
            NSString* Twdlx=[record valueForKey:@"设置属性"];
            
            if([Twdlx isEqualToString:@"中医"])
            {
            self.lbltwd.text = @"体温单格式(中医)";
                self.TwdSwitch.on=YES;
            }
            else
            {
            self.lbltwd.text = @"体温单格式(西医)";
                self.TwdSwitch.on=NO;
            }
            
        }

        
        
    } andFailHandler:^(NSError *error, NSString *title) {
        [[DataEngine sharedInstance]showMessage:[error description] withTitle:@"系统设置读取失败"];
    }];
    
    
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,
                          [UIFont boldSystemFontOfSize:19],UITextAttributeFont,
                          nil];
    
    self.navigationController.navigationBar.titleTextAttributes = dict;

}

-(void)setNav{
    //[TSMessage setDefaultViewController:[Router shared].navigationController];
    
    if([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)])
    {
        [[UINavigationBar appearance] setShadowImage:[UIImage new]];
    }
    
    UINavigationBar *navBar = self.navigationController.navigationBar;
    
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        //if iOS 5.0 and later
        
        [navBar setBackgroundImage:[UIImage imageNamed:@"head-bg.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
}


#pragma UITableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 10.f;
            break;
        case 1:
            return 30.f;
            break;
            
        default:
            break;
    }
    return 0.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 53.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectZero];
    [lbl setBackgroundColor:[UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:235.f/255.f alpha:1.f]];
    switch (section) {
        case 0:
            [lbl setText:@""];
            break;
        case 1:
            [lbl setText:@"  更多选项"];
            
            break;
            
        default:
            break;
    }
    return lbl;
}


#pragma UITableView Datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_muarrSetting count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    return [[_muarrSetting objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];//@"Cell";
    SettingCell * cell = (SettingCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"SettingCell" owner:self options:nil] objectAtIndex:0];
        [cell setSelected:YES animated:YES];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell.lblText setText:[[_muarrSetting objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    if (indexPath.section == 0) {
        if (indexPath.row == 0 || indexPath.row == 1) {
            [cell.settingSwitch setHidden:YES];
            [cell.viewOtherContainer setHidden:NO];
        }
    }
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)logout:(id)sender{
    
    [App gotoLoginPage];
}

- (IBAction)Twdclick:(id)sender {
    //UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [self.TwdSwitch isOn];
    if (isButtonOn) {
        self.lbltwd.text = @"体温单格式(中医)";
        
        
        [[NurseService sharedInstance]queryInsSZ:@"中医" withSuccessHandler:^(id result) {
            [[DataEngine sharedInstance]showMessage:@"系统设置操作成功" withTitle:@"系统设置成功"];
            //[self.navigationController popViewControllerAnimated:YES];
        } andFailHandler:^(NSError *error, NSString *title) {
            [[DataEngine sharedInstance]showMessage:[error description] withTitle:@"系统设置失败"];
        }];

        
        AppDelegate  *appDelegate = App;
       // appDelegate.isScan=NO;
        appDelegate.Twdtype=@"中医";
        

        
//        AddSignsViewController *vc = nil;
//        vc = [[AddSignsViewController alloc] initWithNibName:@"AddSignsViewController" bundle:nil];
//        vc.Twdtype=@"中医";
    }else {
       self.lbltwd.text = @"体温单格式(西医)";

        [[NurseService sharedInstance]queryInsSZ:@"西医" withSuccessHandler:^(id result) {
            [[DataEngine sharedInstance]showMessage:@"系统设置操作成功" withTitle:@"系统设置成功"];
            //[self.navigationController popViewControllerAnimated:YES];
        } andFailHandler:^(NSError *error, NSString *title) {
            [[DataEngine sharedInstance]showMessage:[error description] withTitle:@"系统设置失败"];
        }];
        
        AppDelegate  *appDelegate = App;
        // appDelegate.isScan=NO;
        appDelegate.Twdtype=@"西医";
    }
}
@end
